package Test;


import JaveBean.Manage;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.PropertyConfigurator;
import org.junit.Test;


import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
import java.util.Scanner;

/**
 * @Auther:Liu
 * @Testname:TestMybaties1
 * @Date:2025/3/25 9:58
 */
@SuppressWarnings("ALL")
public class TestMybaties1 {
    public static void main(String[] args) throws Exception {
        TestMybaties1 t = new TestMybaties1();
//        t.insert();
    }

    /**
     * 查询数据
     *
     * @throws IOException
     */
    @Test
    public void findall() throws Exception {
        RZ();
        SqlSession gc = GC();
        //4.执行映射文件中的sql语句
        List<Object> objects = gc.selectList("EmpMapper.findAllDate");
        for (Object O : objects
        ) {
            System.out.println(O);
        }
    }

    /**
     * 插入数据
     *
     * @throws Exception
     */
    public void insert() throws Exception {
        Scanner scanner = new Scanner(System.in);
        RZ();
        SqlSession gc = GC();
        HashMap hashMap = new HashMap();
        System.out.println("请输入要插入的admire:");
        String admire1 = scanner.next();
        hashMap.put("admire", admire1);
        System.out.println("请输入要插入的password:");
        String password1 = scanner.next();
        hashMap.put("password", password1);
        int insert = gc.insert("EmpMapper.insert", hashMap);
        //提交事务
        gc.commit();
    }

    /**
     * 更新数据
     *
     * @throws Exception
     */
    public void update() throws Exception {
        Scanner scanner = new Scanner(System.in);
        RZ();
        SqlSession gc = GC();
        Manage manage = new Manage();
        System.out.println("请输入要修改的admire值:");
        String admire2 = scanner.next();
        manage.setAdmire(admire2);
        System.out.println("请输入要修改的password值:");
        String password2 = scanner.next();
        manage.setPassword(password2);
        System.out.println("请输入要修改对象的id:");
        int id = scanner.nextInt();
        manage.setId(id);
        int update1 = gc.update("EmpMapper.update", manage);
        gc.commit();
    }

    /**
     * 删除数据
     *
     * @throws Exception
     */
    public void delete() throws Exception {
        Scanner scanner = new Scanner(System.in);
        RZ();
        SqlSession gc = GC();
        System.out.println("请输入想要删除对象的id:");
        int id1 = scanner.nextInt();
        int delete = gc.delete("EmpMapper.delete", id1);
        gc.commit();

    }

    /**
     * 查询多列数据
     *
     * @throws Exception
     */
    @Test
    public void FindDate() throws Exception {
        RZ();
        SqlSession gc = GC();
        HashMap hashMap = new HashMap();
        hashMap.put("cols", "admire,id");
        List<Object> objects = gc.selectList("EmpMapper.select3", hashMap);
        for (Object o : objects
        ) {
            System.out.println(o);
        }
    }

    /**
     * 查找一条数据
     *
     * @throws Exception
     */
    public void findone() throws Exception {
        Scanner scanner = new Scanner(System.in);
        RZ();
        SqlSession gc = GC();
        System.out.println("请输入要查找数据的id:");
        int id3 = scanner.nextInt();
        Object o = gc.selectOne("EmpMapper.selectone", id3);
        System.out.println(o);
    }

    /**
     * 查名字中包含某个字的
     *
     * @throws Exception
     */
    @Test
    public void findname() throws Exception {
        RZ();
        SqlSession gc = GC();
        HashMap hashMap = new HashMap();
        hashMap.put("admire", "%芙%");
        List<Object> objects = gc.selectList("EmpMapper.findname", hashMap);
        for (Object o : objects
        ) {
            System.out.println(o);
        }
    }

    /**
     * 给定范围查找数据
     *
     * @throws Exception
     */
    @Test
    public void findid() throws Exception {
        RZ();
        SqlSession gc = GC();
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("minid", 20);
        hashMap.put("maxid", 30);
        List<Object> objects = gc.selectList("EmpMapper.findid", hashMap);
        for (Object o : objects
        ) {
            System.out.println(o);
        }
    }

    /**
     * 批量删除
     *
     * @throws Exception
     */
    @Test
    public void deleteids() throws Exception {
        RZ();
        SqlSession gc = GC();
        Integer[] ids = {20, 13};
        int delete = gc.delete("EmpMapper.deleteids", ids);
        gc.commit();
    }

    /**
     * 加载日志
     *
     * @throws Exception
     */
    public void RZ() throws Exception {
        //输出日志信息
        Properties properties = new Properties();
        FileInputStream fileInputStream = new FileInputStream("src/main/resources/config/log4j.properties");
        properties.load(fileInputStream);
        PropertyConfigurator.configure(properties);
    }

    /**
     * 获取工厂对象
     *
     * @return
     * @throws Exception
     */
    public SqlSession GC() throws Exception {
        //1.读取mybatis配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("config/Mybatiesconfig1.xml");
        //2.读取sqlSession工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //3.通过工厂获取对象
        SqlSession sqlSession = build.openSession();
        return sqlSession;
    }
}
